#include "Deck.H"
#include "Card.H"
#include <cstdlib>
#include <ctime>
#include <sys/time.h>
#define NUMBER_OF_SUITS           4
#define MAXIMUM_NUMBUER_OF_CARDS 52
Deck::Deck()
{
    //CreateCards();
}
void Deck::CreateCards()
{
    while ( mCards.size() )
    {
        delete mCards.back();
        mCards.pop_back();
    }
    UINT16 Suits[ NUMBER_OF_SUITS ] = {'C','D','H', 'S' };
    UINT16 points; 
    for ( UINT16 suit=0; suit < NUMBER_OF_SUITS; suit++ )
    {
        for ( UINT16 rank=1; rank <= 13; rank++ )
        {
            if (  Suits[suit] == 'D' && rank == 10 ) points=6;
            else if ( rank == 1) points = 1;
            else if ( Suits[suit] == 'S' ) points = rank;
            else points = 0  ;
            mCards.push_back( new Card( Suits[suit], rank, points ));
        }
    }
}

void Deck::Suffle()
{
    CreateCards();
    UINT16 rand_location = 0;
    Card *tmp = NULL;
    for ( int i=0; i < MAXIMUM_NUMBUER_OF_CARDS ; i++ )
    {
		struct timeval t1;
		gettimeofday(&t1, NULL);
		srand(t1.tv_usec * t1.tv_sec);
		
        //srand( time(NULL)*(i+3)*(rand_location+1) );
        rand_location = rand() % MAXIMUM_NUMBUER_OF_CARDS;
		//std::cout << "location " << rand_location << "\n";
        tmp = mCards.at(i);
        mCards [ i ] = mCards.at( rand_location );
        mCards [ rand_location ] = tmp;
    }
    std::cout<< "Suffling is done" << std::endl;
}

void Deck::Deal( UINT16 num, std::vector<Card*> *cards )
{
    if ( mCards.size() < num )
    {
        return;
    }
    for ( UINT16 i = 0; i < num; i++ )
    {
        cards->push_back( mCards.back() );
        mCards.pop_back();
    }
};
void Deck::Dump()
{
	for (UINT16 i=0; i<mCards.size(); i++) {
		mCards[i]->Dump();
		std::cout << "\n";
	}
}

